package com.mlf.prototype.spring;

import com.mlf.prototype.demo.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;

@RestController
public class OrdersController {

    @PostMapping("/deepClone")
    public Orders deepClone() {
        Orders orders = getOrders();
        Orders clone = orders.clone();
        return clone;
    }

    @PostMapping("/shallowClone")
    public Orders shallowClone() {
        Orders orders = getOrders();
        Orders clone = orders.shallowClone();
        return clone;
    }

    public Orders getOrders() {
        Orders orders = new Orders();
        orders.setId(1L);
        orders.setItems(new ArrayList<>(Arrays.asList("item1", "item2")));
        return orders;
    }

    @Autowired
    private ApplicationContext applicationContext;

    @PostMapping("/prototype")
    public void prototype() {
        Orders orders1 = applicationContext.getBean(Orders.class);
        Orders orders2 = applicationContext.getBean(Orders.class);
        System.out.println("orders1 == orders2 : " + (orders1 == orders2));
    }

}
